package edu.pw.p2pclient.fragment;

import edu.pw.p2pclient.R;
import edu.pw.p2pclient.data.SharedTreeAdapter;
import edu.pw.p2pclient.data.TreeNode;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class PushUploadFragment extends Fragment {

	public static final String TITLE = "Pushowane";

	private ListView mListView;
	private SharedTreeAdapter mAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.push_upload_fragment,
				container, false);
		mListView = (ListView) rootView.findViewById(R.id.pushUploadListView);
		mAdapter = new SharedTreeAdapter(container.getContext(), true);
		mListView.setAdapter(mAdapter);
		setHasOptionsMenu(true);
		return rootView;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.fragment_push, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public void onResume() {

		mAdapter.refreshAdapter();
		super.onResume();
	}

	public boolean onBackPressed() {

		if (mAdapter == null
				|| mAdapter.getCurrentFolder().equals(TreeNode.ROOT_PATH))
			return false;
		else {
			mAdapter.showDirectory(mAdapter.getParentFolder());
			return true;
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.actionChangeView:
			changeView();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void changeView() {

		mAdapter.changeView();
	}
}
